<?php
/**
 * Created by PhpStorm.
 * User: spring
 * Date: 19-6-26
 * Time: 下午1:59
 * 梦想礼盒接口
 */

namespace App\Services;


class DreamBoxService
{
    public $domin,$is_dev;

    public function __construct()
    {
        $this->domin = env('DREAMBOX');
        $this->is_dev = 1; // 开发调试开关，关闭后不保存日志
    }

    /**
     * 获取八贝币数量
     * @param $openid
     */
    public function coin($openid){
        $info = curlPost($this->domin . 'account/coin',['openid'=>$openid]);
        if($this->is_dev){
            w_log('查询八贝币数量，[' . $openid .']' . "\n" . $info, 'dream_box/search');
        }

        if($info){
            $result = json_decode($info,true);
            if($result['code'] == 200){
                return $result['data'];
            }
        }
        return 0;
    }
    /**
     * 获取贝壳数量
     * @param $openid
     */
    public function consh($openid){
        $info = curlPost($this->domin . 'account/bk',['openid'=>$openid]);
        if($this->is_dev){
            w_log('查询贝壳数量，[' . $openid .']' . "\n" . $info, 'dream_box/search');
        }

        if($info){
            $result = json_decode($info,true);
            if($result['code'] == 200){
                return $result['data'];
            }
        }
        return 0;
    }

    /**
     * 获取八贝币及贝壳数量及最后更新时间
     * @param $openid
     */
    public function coin_consh($openid){
        $info = curlPost($this->domin . 'account/notice',['openid'=>$openid]);
        if($this->is_dev){
            w_log('查询八贝币和贝壳提醒，[' . $openid .']' . "\n" . $info, 'dream_box/search');
        }
        $infos['bi'] = 0;
        $infos['ke'] = 0;
        $infos['bi_update'] = '';
        $infos['ke_update'] = '';
        if($info){
            $result = json_decode($info,true);
            if($result['code'] == 200){
                $infos['bi'] = $result['data']['coin'];
                $infos['ke'] = $result['data']['bk'];
                $infos['bi_update'] = $result['data']['coin_update_time'];
                $infos['ke_update'] = $result['data']['bk_update_time'];
            }
        }
        return $infos;
    }

    /**
     * 获取我的八贝币明细
     * @param $openid
     * @param $page
     */
    public function coinList($openid, $page = 1){
        $info = curlPost($this->domin . 'coin/list',['openid'=>$openid,'page'=>$page]);
        if($this->is_dev){
            w_log('获取我的八贝币明细，[' . $openid .']' . "\n" . $info, 'dream_box/search');
        }

        if($info){
            $result = json_decode($info,true);
            if($result['code'] == 200){
                return $result['data'];
            }else{
               return '';
            }
        }

    }


    /**
     * 获取我的贝壳明细
     * @param $openid
     * @param $page
     */
    public function conshList($openid, $page){
        $info = curlPost($this->domin . 'bk/list',['openid'=>$openid,'page'=>$page]);
        if($this->is_dev){
            w_log('获取我的八贝币明细，[' . $openid .']' . "\n" . $info, 'dream_box/search');
        }

        if($info){
            $result = json_decode($info,true);
            if($result['code'] == 200){
                return $result['data'];
            }else{
               return '';
            }
        }

    }

    /**
     * 用户充值获取贝壳
     * @param $openid 用户openid
     * @param $money 充值金额
     * @param $is_first 是否为首充 1首充
     * @param $inviter_openid 推荐人openid
     * @return int
     */
    public function bk_recharge($openid, $money, $is_first = 0, $inviter_openid)
    {
        $url = $this->domin . 'bk_act/recharge';
        $data = array(
            'openid'         => $openid,
            'money'          => $money,
        );
        if ($is_first == 1) {
            $data['is_first'] = $is_first;
            $data['inviter_openid'] = $inviter_openid;
        }
        $info = curlPost($url, $data);

        if ($this->is_dev) {
            w_log('用户充值获取贝壳，[' . $openid . ']' . "\n" . json_encode($data), 'dream_box/recharge');
            w_log('用户充值获取贝壳，[' . $openid . ']' . "\n" . $info, 'dream_box/recharge');
        }
        $info = json_decode($info, true);
        if (array_get($info, 'code')) {
            return true;
        }
        return false;
    }

    /**
     * 用户消费获取贝壳
     * @param $openid
     * @param $money 消费金额
     * @param $order_id 订单号
     * @return int
     */
    public function bk_pay($openid,$money,$order_no){
        $info = curlPost($this->domin . 'bk_act/buy',['openid'=>$openid,'money'=>$money*100,'order_id'=>$order_no]);
        w_log('用户消费获取贝壳，[' . $order_no .']' . "\n" . json_encode(['openid'=>$openid,'money'=>$money*100,'order_id'=>$order_no]), 'dream_box/bk_act');
        if($this->is_dev){
            w_log('用户消费获取贝壳，[' . $order_no .']' . "\n" . $info, 'dream_box/bk_act');
        }
        if($info){
            $result = json_decode($info,true);
            if($result['code'] == 200){
                return 1;
            }else{
                return $result['message'];
            }
        }
        return '接口失败';
    }

    /**
     * 八贝币订单创建
     * @param $openid
     * @param $order_no 订单号
     * @param $price 订单原价
     * @param $pay_price 订单支付价格
     * @param $coin 八贝币支付价格
     * @return int|string
     */
    public function coin_order($openid,$order_no,$price,$pay_price,$coin){
        $info = curlPost($this->domin . 'coin/create_order',['openid'=>$openid,'order'=>$order_no,'price'=>$price,'pay_price'=>$pay_price,'coin'=>$coin]);
        if($this->is_dev){
            w_log('八贝币订单创建，[' . $order_no .']' . "\n" . $info, 'dream_box/order');
        }
        if($info){
            $result = json_decode($info,true);
            if($result['code'] == 200){
                return 1;
            }else{
                return $result['message'];
            }
        }
        return '接口失败';
    }


    /**
     * 八贝币订单支付
     * @param $openid
     * @param $order_no 订单号
     * @return int|string
     */
    public function coin_pay($openid,$order_no){
        $info = curlPost($this->domin . 'coin/pay_order',['openid'=>$openid,'order'=>$order_no]);
        if($this->is_dev){
            w_log('八贝币订单支付，[' . $order_no .']' . "\n" . $info, 'dream_box/order');
        }
        if($info){
            $result = json_decode($info,true);
            if($result['code'] == 200){
                return 1;
            }else{
                return $result['message'];
            }
        }
        return '接口失败';
    }
    /**
     * 八贝币订单退款
     * @param $openid
     * @param $order_no 订单号
     * @return int|string
     */
    public function coin_refund($openid,$order_no){
        $info = curlPost($this->domin . 'coin/refund_order',['openid'=>$openid,'order'=>$order_no]);
        if($this->is_dev){
            w_log('八贝币订单退款，[' . $order_no .']' . "\n" . $info, 'dream_box/order');
        }
        if($info){
            $result = json_decode($info,true);
            if($result['code'] == 200){
                return 1;
            }else{
                return $result['message'];
            }
        }
        return '接口失败';
    }
    /**
     * 八贝币支付取消订单
     * @param $openid
     * @param $order_no
     * @return int
     * 待完成
     */
    public function coin_cancel($openid,$order_no){
        $info = curlPost($this->domin . 'coin/cancel_order',['openid'=>$openid,'order'=>$order_no]);
        if($this->is_dev){
            w_log('八贝币订单支付，[' . $order_no .']' . "\n" . $info, 'dream_box/order');
        }
        if($info){
            $result = json_decode($info,true);
            if($result['code'] == 200){
                return 1;
            }else{
                return $result['message'];
            }
        }
        return 0;
    }
}